package tmall.domain.service;

import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;
import tmall.common.config.AppConst;
import tmall.common.exception.CustomGenericException;
import tmall.common.mapper.BaseMapper;
import tmall.common.redis.RedisService;
import tmall.common.service.BaseService;
import tmall.common.util.EmailUtils;
import tmall.domain.entity.User;
import tmall.domain.mapper.UserMapper;
import tmall.domain.vo.UserVO;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

/**
 * @author zhangservlet
 * @description: TODO
 * @date 2021/7/9
 */
@Service
public class UserService extends BaseService<User> {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisService redisService;

    @Override
    protected BaseMapper<User> getMapper() {
        return this.userMapper;
    }

    public User login(String username, String password) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("username", username);
        params.put("password", password);
        User user = userMapper.findBy(params);
        return user;
    }

    public boolean register(UserVO vo) {
        Object securityCode;
        // 注册时校验验证码，暂时只支持邮箱验证
        securityCode = redisService.get(vo.getEmail());
        if (Objects.isNull(securityCode)) {
            throw new CustomGenericException("验证码已过期");
        }
        if (!securityCode.equals(vo.getSecurityCode())) {
            throw new CustomGenericException("验证码错误");
        }
        userMapper.insert(vo);
        return true;
    }

    /**
     * 发送邮件验证码
     *
     * @param email
     */
    public void sendSecurityCode(String email) {
        // 从redis中获取对应邮箱的验证码，若验证码过期则重新生成
        Object securityCode = redisService.get(email);
        if (Objects.isNull(securityCode)) {
            securityCode = this.getSecurityCode();
            // 过期时间10分钟
            redisService.set(email, securityCode, 60 * 10);
        }
        EmailUtils.send(email, "确认您的验证信息", "您的验证码是: " + securityCode);
    }

    private String getSecurityCode() {
        Random ran = new Random();
        int num = ran.nextInt(999999);
        return String.format("%06d", num);
    }

    public boolean checkDuplicate(UserVO vo) {
        // 注册前先校验用户名、手机号、邮箱是否重复
        Map<String, Object> params = Maps.newHashMap();
        params.put("columnName", "username");
        params.put("columnValue", vo.getUsername());
        if (userMapper.checkDuplicate(params) > 0) {
            throw new CustomGenericException("该用户名已被使用");
        }

        if (AppConst.REGISTER_PHONE.equals(vo.getRegisterMethod())) {
            params.put("columnName", "phone");
            params.put("columnValue", vo.getPhone());
            if (userMapper.checkDuplicate(params) > 0) {
                throw new CustomGenericException("该手机号已注册");
            }
        }
        if (AppConst.REGISTER_EMAIL.equals(vo.getRegisterMethod())) {
            params.put("columnName", "email");
            params.put("columnValue", vo.getEmail());
            if (userMapper.checkDuplicate(params) > 0) {
                throw new CustomGenericException("该邮箱已注册");
            }
        }
        return true;
    }
}
